@@ -0,0 +1,82 @@  | 
            ||
| 1 | 
                +# -*- coding: utf-8 -*-  | 
            |
| 2 | 
                +  | 
            |
| 3 | 
                +import logging  | 
            |
| 4 | 
                +  | 
            |
| 5 | 
                +from django.conf import settings  | 
            |
| 6 | 
                +from django.db import transaction  | 
            |
| 7 | 
                +from django_redis_connector import connector  | 
            |
| 8 | 
                +from django_six import CompatibilityBaseCommand, close_old_connections  | 
            |
| 9 | 
                +from django_we.models import SubscribeUserInfo  | 
            |
| 10 | 
                +from pywe_storage import RedisStorage  | 
            |
| 11 | 
                +from pywe_user import get_user_info  | 
            |
| 12 | 
                +  | 
            |
| 13 | 
                +from account.models import UserInfo  | 
            |
| 14 | 
                +from mch.models import BrandInfo  | 
            |
| 15 | 
                +from utils.redis.rkeys import SUBSCRIBE_USERINFO_LIST  | 
            |
| 16 | 
                +  | 
            |
| 17 | 
                +  | 
            |
| 18 | 
                +r1 = connector(settings.REDIS.get('db1', {}))
               | 
            |
| 19 | 
                +  | 
            |
| 20 | 
                +  | 
            |
| 21 | 
                +WECHAT = settings.WECHAT  | 
            |
| 22 | 
                +  | 
            |
| 23 | 
                +  | 
            |
| 24 | 
                +logger = logging.getLogger('console')
               | 
            |
| 25 | 
                +  | 
            |
| 26 | 
                +  | 
            |
| 27 | 
                +class Command(CompatibilityBaseCommand):  | 
            |
| 28 | 
                + def handle(self, *args, **options):  | 
            |
| 29 | 
                +  | 
            |
| 30 | 
                +        logger.info('SubscribeUserInfo is dealing')
               | 
            |
| 31 | 
                +  | 
            |
| 32 | 
                + brands = BrandInfo.objects.filter(status=True)  | 
            |
| 33 | 
                + subscribe_keys = [SUBSCRIBE_USERINFO_LIST % brand.brand_id for brand in brands]  | 
            |
| 34 | 
                +  | 
            |
| 35 | 
                + while True:  | 
            |
| 36 | 
                +            # r.rpushjson('SUBSCRIBE_USERINFO_LIST', {
               | 
            |
| 37 | 
                + # 'brand_id': 'brand_id',  | 
            |
| 38 | 
                + # 'openid': 'openid',  | 
            |
| 39 | 
                + # })  | 
            |
| 40 | 
                + k, v = r1.blpopjson(subscribe_keys, 60)  | 
            |
| 41 | 
                + if not v:  | 
            |
| 42 | 
                + continue  | 
            |
| 43 | 
                +  | 
            |
| 44 | 
                + logger.info(v)  | 
            |
| 45 | 
                +  | 
            |
| 46 | 
                +            brand_id, openid = v.get('brand_id', ''), v.get('openid', '')
               | 
            |
| 47 | 
                +  | 
            |
| 48 | 
                +            wxcfg = WECHAT.get('{}:JSAPI'.format(brand_id), {})
               | 
            |
| 49 | 
                +  | 
            |
| 50 | 
                +            appid = wxcfg.get('appID')
               | 
            |
| 51 | 
                +            secret = wxcfg.get('appsecret')
               | 
            |
| 52 | 
                +  | 
            |
| 53 | 
                + userinfo = get_user_info(openid, appid=appid, secret=secret, storage=RedisStorage(r1))  | 
            |
| 54 | 
                +  | 
            |
| 55 | 
                + close_old_connections()  | 
            |
| 56 | 
                +  | 
            |
| 57 | 
                + with transaction.atomic():  | 
            |
| 58 | 
                +                subscribe, unionid, openid = userinfo.get('subscribe', ''), userinfo.get('unionid', ''), userinfo.get('openid', '')
               | 
            |
| 59 | 
                + if subscribe:  | 
            |
| 60 | 
                +                    SubscribeUserInfo.objects.update_or_create(extraid=brand_id, openid=openid, defaults={
               | 
            |
| 61 | 
                + 'unionid': unionid,  | 
            |
| 62 | 
                +                        'nickname': userinfo.get('nickname', ''),
               | 
            |
| 63 | 
                +                        'sex': userinfo.get('sex', ''),
               | 
            |
| 64 | 
                +                        'headimgurl': userinfo.get('headimgurl', ''),
               | 
            |
| 65 | 
                +                        'country': userinfo.get('country', ''),
               | 
            |
| 66 | 
                +                        'province': userinfo.get('province', ''),
               | 
            |
| 67 | 
                +                        'city': userinfo.get('city', ''),
               | 
            |
| 68 | 
                +                        'subscribe': userinfo.get('subscribe', ''),
               | 
            |
| 69 | 
                +                        'subscribe_time': userinfo.get('subscribe_time', ''),
               | 
            |
| 70 | 
                +                        'subscribe_scene': userinfo.get('subscribe_scene', ''),
               | 
            |
| 71 | 
                +                        'groupid': userinfo.get('groupid', ''),
               | 
            |
| 72 | 
                +                        'tagid_list': userinfo.get('tagid_list', ''),
               | 
            |
| 73 | 
                +                        'qr_scene': userinfo.get('qr_scene', ''),
               | 
            |
| 74 | 
                +                        'qr_scene_str': userinfo.get('qr_scene_str', ''),
               | 
            |
| 75 | 
                +                        'language': userinfo.get('language', ''),
               | 
            |
| 76 | 
                +                        'remark': userinfo.get('remark', ''),
               | 
            |
| 77 | 
                + })  | 
            |
| 78 | 
                + UserInfo.objects.filter(unionid=unionid).update(openid=openid)  | 
            |
| 79 | 
                + else:  | 
            |
| 80 | 
                + SubscribeUserInfo.objects.filter(extraid=brand_id, openid=openid).update(subscribe=subscribe)  | 
            |
| 81 | 
                +  | 
            |
| 82 | 
                + close_old_connections()  | 
            
                @@ -86,10 +86,17 @@ def DJANGO_WE_COMPONENT_CALLBACK_FUNC(request, appid, data, decrypted=None):  | 
            ||
| 86 | 86 | 
                })  | 
            
| 87 | 87 | 
                 | 
            
| 88 | 88 | 
                # 腾龙  | 
            
| 89 | 
                - if event == 'user_get_card':  | 
            |
| 90 | 
                - UserInfo.objects.filter(unionid=unionid).update(openid=openid, has_membercard=True, membercardid=membercardid, memberusercardcode=memberusercardcode)  | 
            |
| 91 | 
                - elif event == 'user_del_card':  | 
            |
| 92 | 
                - UserInfo.objects.filter(memberusercardcode=memberusercardcode).update(has_membercard=False)  | 
            |
| 89 | 
                + if tousername == 'gh_c87efc299ce5':  | 
            |
| 90 | 
                + if event == 'user_get_card':  | 
            |
| 91 | 
                + UserInfo.objects.filter(unionid=unionid).update(openid=openid, has_membercard=True, membercardid=membercardid, memberusercardcode=memberusercardcode)  | 
            |
| 92 | 
                + elif event == 'user_del_card':  | 
            |
| 93 | 
                + UserInfo.objects.filter(memberusercardcode=memberusercardcode).update(has_membercard=False)  | 
            |
| 94 | 
                + elif event == 'subscribe' or event == 'unsubscribe':  | 
            |
| 95 | 
                + brand_id = settings.COMPONENT_CALLBACK_CONFIG[tousername]  | 
            |
| 96 | 
                +            r.rpushjson(SUBSCRIBE_USERINFO_LIST % brand_id, {
               | 
            |
| 97 | 
                + 'brand_id': brand_id,  | 
            |
| 98 | 
                + 'openid': openid,  | 
            |
| 99 | 
                + })  | 
            |
| 93 | 100 | 
                 | 
            
| 94 | 101 | 
                 | 
            
| 95 | 102 | 
                def DJANGO_WE_REDIS_OBJ_FUNC(request):  | 
            
                @@ -0,0 +1,6 @@  | 
            ||
| 1 | 
                +# -*- coding: utf-8 -*-  | 
            |
| 2 | 
                +from __future__ import unicode_literals  | 
            |
| 3 | 
                +  | 
            |
| 4 | 
                +from django.contrib import admin  | 
            |
| 5 | 
                +  | 
            |
| 6 | 
                +# Register your models here.  | 
            
                @@ -0,0 +1,8 @@  | 
            ||
| 1 | 
                +# -*- coding: utf-8 -*-  | 
            |
| 2 | 
                +from __future__ import unicode_literals  | 
            |
| 3 | 
                +  | 
            |
| 4 | 
                +from django.apps import AppConfig  | 
            |
| 5 | 
                +  | 
            |
| 6 | 
                +  | 
            |
| 7 | 
                +class ShellsConfig(AppConfig):  | 
            |
| 8 | 
                + name = 'shells'  | 
            
                @@ -0,0 +1,6 @@  | 
            ||
| 1 | 
                +# -*- coding: utf-8 -*-  | 
            |
| 2 | 
                +from __future__ import unicode_literals  | 
            |
| 3 | 
                +  | 
            |
| 4 | 
                +from django.db import models  | 
            |
| 5 | 
                +  | 
            |
| 6 | 
                +# Create your models here.  | 
            
                @@ -0,0 +1,6 @@  | 
            ||
| 1 | 
                +# -*- coding: utf-8 -*-  | 
            |
| 2 | 
                +from __future__ import unicode_literals  | 
            |
| 3 | 
                +  | 
            |
| 4 | 
                +from django.test import TestCase  | 
            |
| 5 | 
                +  | 
            |
| 6 | 
                +# Create your tests here.  | 
            
                @@ -0,0 +1,33 @@  | 
            ||
| 1 | 
                +# -*- coding: utf-8 -*-  | 
            |
| 2 | 
                +  | 
            |
| 3 | 
                +from django.conf import settings  | 
            |
| 4 | 
                +from django_redis_connector import connector  | 
            |
| 5 | 
                +from pywe_storage import RedisStorage  | 
            |
| 6 | 
                +from pywe_user import get_all_users  | 
            |
| 7 | 
                +  | 
            |
| 8 | 
                +from utils.redis.rkeys import SUBSCRIBE_USERINFO_LIST  | 
            |
| 9 | 
                +  | 
            |
| 10 | 
                +  | 
            |
| 11 | 
                +r1 = connector(settings.REDIS.get('db1', {}))
               | 
            |
| 12 | 
                +  | 
            |
| 13 | 
                +  | 
            |
| 14 | 
                +WECHAT = settings.WECHAT  | 
            |
| 15 | 
                +  | 
            |
| 16 | 
                +  | 
            |
| 17 | 
                +def fetch_users_func(authorizer_appid, infos):  | 
            |
| 18 | 
                + print authorizer_appid, infos  | 
            |
| 19 | 
                +    openids = infos.get('data', {}).get('openid', [])
               | 
            |
| 20 | 
                + for openid in openids:  | 
            |
| 21 | 
                +        r1.rpushjson(SUBSCRIBE_USERINFO_LIST % authorizer_appid, {
               | 
            |
| 22 | 
                + 'brand_id': authorizer_appid,  | 
            |
| 23 | 
                + 'openid': openid  | 
            |
| 24 | 
                + })  | 
            |
| 25 | 
                +  | 
            |
| 26 | 
                +  | 
            |
| 27 | 
                +def fetch_all_users(brand_id):  | 
            |
| 28 | 
                +    wxcfg = WECHAT.get('{}:JSAPI'.format(brand_id), {})
               | 
            |
| 29 | 
                +  | 
            |
| 30 | 
                +    appid = wxcfg.get('appID')
               | 
            |
| 31 | 
                +    secret = wxcfg.get('appsecret')
               | 
            |
| 32 | 
                +  | 
            |
| 33 | 
                + get_all_users(appid=appid, secret=secret, storage=RedisStorage(r1), authorizer_appid=brand_id, users_func=fetch_users_func)  |